package com.carskeeper.service;
import java.math.BigDecimal;

import com.alibaba.dubbo.config.annotation.Service;
import com.carskeeper.body.service.BodyProviderService;
import com.carskeeper.common.dtos.AppHttpCodeEnum;
import com.carskeeper.common.dtos.PageResponseResult;
import com.carskeeper.common.dtos.ResponseResult;
import com.carskeeper.common.exception.AcException;
import com.carskeeper.mapper.BodyProviderMapper;
import com.carskeeper.mapper.BodyUserMapper;
import com.carskeeper.model.body.dtos.BodyProviderDto;
import com.carskeeper.model.body.dtos.BodyUserCountDto;
import com.carskeeper.model.body.dtos.QueryProviderDto;
import com.carskeeper.model.body.dtos.UpdateProvideStatusDto;
import com.carskeeper.model.body.pojos.BodyProvider;
import com.carskeeper.model.body.vo.BodyProviderListUserVo;
import com.carskeeper.model.body.vo.BodyProviderListVo;
import com.carskeeper.model.body.vo.BodyServiceVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Component
public class BodyProviderServiceImpl implements BodyProviderService {

    @Autowired
    private BodyProviderMapper bodyProviderMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private BodyUserMapper bodyUserMapper;

    @Override
    public PageResponseResult<List<BodyProviderListVo>> providerList(QueryProviderDto dto) {
        //校验数据
        if (dto==null||dto.getProviderType()==null){
            throw new AcException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //初始化页数
        if (dto.getPage()==null||dto.getPage()==0) dto.setPage(1);
        if (dto.getPageSize()==null||dto.getPageSize()==0) dto.setPageSize(10);
        //调用mapper查询
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        List<BodyProvider> bodyProviderList =  bodyProviderMapper.providerList(dto);
        PageInfo<BodyProvider> bodyProviderPageInfo = new PageInfo<>(bodyProviderList);
        //数据转换Vo
        ArrayList<BodyProviderListVo> bodyProviderListVos = new ArrayList<>();
        for (BodyProvider bodyProvider : bodyProviderList) {
            BodyProviderListVo bodyProviderListVo = new BodyProviderListVo();
            bodyProviderListVo.setProviderNo(bodyProvider.getProviderNo());
            bodyProviderListVo.setSimpleName(bodyProvider.getSimpleName());
            bodyProviderListVo.setContactName(bodyProvider.getContactName());
            bodyProviderListVo.setContactPhone(bodyProvider.getContactPhone());
            bodyProviderListVo.setDistrict(bodyProvider.getDistrict());
            bodyProviderListVo.setStatus(bodyProvider.getStatus());
            bodyProviderListVos.add(bodyProviderListVo);
        }
        PageResponseResult<List<BodyProviderListVo>> listPageResponseResult = new PageResponseResult<>(bodyProviderPageInfo.getPageNum(), bodyProviderPageInfo.getPageSize(), Math.toIntExact(bodyProviderPageInfo.getTotal()));
        listPageResponseResult.setData(bodyProviderListVos);
        return listPageResponseResult;
    }

    @Override
    public ResponseResult updateProviderStatus(UpdateProvideStatusDto dto) {
        //参数校验
        if (dto==null||dto.getProviderNo()==null||dto.getStatus()==null) {
            throw new AcException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //调用mapper修改
        bodyProviderMapper.updateProviderStatus(dto);
        return ResponseResult.okResult(null);
    }

    @Override
    public ResponseResult<BodyServiceVo> findService(Long id) {
        //参数校验
        if (id==null){
            throw new AcException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //调用mapper修改
        BodyServiceVo bodyServiceVo = bodyProviderMapper.findService(id);
        return  ResponseResult.okResult(bodyServiceVo);
    }

    @Override
    public ResponseResult saveProvider(BodyProviderDto dto) {
        //校验参数
        if (dto==null) {
            throw new AcException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //判断是添加还是修改

        BodyProvider bodyProvider = new BodyProvider();
        bodyProvider.setFullName(dto.getFullName());
        bodyProvider.setSimpleName(dto.getSimpleName());
        bodyProvider.setContactName(dto.getContactName());
        bodyProvider.setContactPhone(dto.getContactPhone());
        bodyProvider.setAddress(dto.getAddress());
        bodyProvider.setLng(dto.getLng());
        bodyProvider.setLat(dto.getLat());
        bodyProvider.setProviderType(0);
        bodyProvider.setDistrict(dto.getDistrict());
        if (dto.getId()==null) {
            //新增
            Long key = redisTemplate.boundValueOps("PROVIDER_NO").increment();
            bodyProvider.setProviderNo(Math.toIntExact(key));
            bodyProvider.setStatus(false);
            bodyProvider.setIsAudited(0);
            //是服务商还是配件商
            if (dto.getProviderType()==0){
                //服务商
                bodyProvider.setIsChain(dto.getIsChain());
                bodyProvider.setSignType(dto.getSignType());
            }else {
                //配件商
                bodyProvider.setIsChain(false);
                bodyProvider.setSignType(0);
            }
            bodyProvider.setCreateTime(new Date());
            bodyProvider.setUpdateTime(new Date());
            bodyProviderMapper.addProvider(bodyProvider);
        }else {
            //修改
            bodyProvider.setId(dto.getId());
            bodyProvider.setProviderNo(dto.getProviderNo());
            bodyProvider.setIsChain(dto.getIsChain());
            bodyProvider.setSignType(dto.getSignType());
            bodyProvider.setUpdateTime(new Date());
            bodyProviderMapper.updateProvider(bodyProvider);
        }
        return ResponseResult.okResult(null);
    }

    @Override
    public PageResponseResult<List<BodyProviderListUserVo>> providerBodyList(QueryProviderDto dto) {
        //调用本类方法查询出提供商列表
        PageResponseResult<List<BodyProviderListVo>> listPageResponseResult = this.providerList(dto);
        List<BodyProviderListVo> bodyProviderListVoList = listPageResponseResult.getData();
        //提取provider_id查询主体用户
        List<Integer> providerNoList = bodyProviderListVoList.stream().map(BodyProviderListVo::getProviderNo).collect(Collectors.toList());
        //查询得到每个主体用户的数量
        List<BodyUserCountDto> bodyBodyUserByNoList = bodyUserMapper.findBodyBodyUserByNoList(providerNoList);
        //HashSet<BodyUserCountDto> bodyUserCountSet = new HashSet<>(bodyBodyUserByNoList);
        Map<String, Integer> bodyBodyUserMap = bodyBodyUserByNoList.stream().collect(Collectors.toMap(BodyUserCountDto::getProviderNo, BodyUserCountDto::getCount));

        //组装数据
        List<BodyProviderListUserVo> bodyProviderListUserVos = bodyProviderListVoList.stream().map(bodyProviderListVo -> {
            BodyProviderListUserVo bodyProviderListUserVo = new BodyProviderListUserVo();
            bodyProviderListUserVo.setProviderNo(bodyProviderListVo.getProviderNo());
            bodyProviderListUserVo.setSimpleName(bodyProviderListVo.getSimpleName());
            bodyProviderListUserVo.setDistrict(bodyProviderListVo.getDistrict());
            bodyProviderListUserVo.setType(bodyProviderListVo.getType());
            bodyProviderListUserVo.setStatus(bodyProviderListVo.getStatus() ? "正常" : "锁定");
            bodyProviderListUserVo.setOperation(0);
            if (bodyBodyUserMap.containsKey(bodyProviderListVo.getProviderNo().toString()) && bodyBodyUserMap.get(bodyProviderListVo.getProviderNo().toString()) > 0) {
                bodyProviderListUserVo.setOperation(1);
            }
            return bodyProviderListUserVo;
        }).collect(Collectors.toList());

        PageResponseResult<List<BodyProviderListUserVo>> bodyProviderListUserVoPageResponseResult = new PageResponseResult<>(listPageResponseResult.getCurrentPage(), listPageResponseResult.getCurrentPage(), listPageResponseResult.getTotal());
        bodyProviderListUserVoPageResponseResult.setData(bodyProviderListUserVos);

        return bodyProviderListUserVoPageResponseResult;
    }
}
